<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button
            v-hasPerm="['system:lpsOrderXcp:add']"
            type="success"
            @click="handleOpenDialog()"
        >
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button
            v-hasPerm="['system:lpsOrderXcp:delete']"
            type="danger"
            :disabled="removeIds.length === 0"
            @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </div>

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="index" label="序号" width="60" align="center"/>
                    <el-table-column
                        key="cgdd"
                        label="采购订单"
                        prop="cgdd"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="poxxm"
                        label="PO行项目"
                        prop="poxxm"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="xsdd"
                        label="销售订单"
                        prop="xsdd"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="soxxm"
                        label="SO行项目"
                        prop="soxxm"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="wl"
                        label="物料"
                        prop="wl"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="ms"
                        label="描述"
                        prop="ms"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="thbb"
                        label="图号/版本"
                        prop="thbb"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="sl"
                        label="数量"
                        prop="sl"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="jhrq"
                        label="交货日期"
                        prop="jhrq"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="jgdbz"
                        label="激光打标字"
                        prop="jgdbz"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="bk"
                        label="BK"
                        prop="bk"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="tk"
                        label="TK"
                        prop="tk"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="gq"
                        label="GQ"
                        prop="gq"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00Tsd"
                        label="P00_TSD"
                        prop="p00Tsd"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="code"
                        label="CODE"
                        prop="code"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00RetainerType"
                        label="P00_RETAINER_TYPE"
                        prop="p00RetainerType"
                        min-width="180"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00Intercom"
                        label="P00_INTERCOM"
                        prop="p00Intercom"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00ApronType"
                        label="P00_APRON_TYPE"
                        prop="p00ApronType"
                        min-width="160"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00TbType"
                        label="P00_TB_TYPE"
                        prop="p00TbType"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="p00StructureType"
                        label="P00_STRUCTURE_TYPE"
                        prop="p00StructureType"
                        min-width="190"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="drivePos"
                        label="DRIVE_POS"
                        prop="drivePos"
                        min-width="150"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        key="bz"
                        label="备注"
                        prop="bz"
                        width="300"
                        align="center"
                        :show-overflow-tooltip="true"
                    />
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template #default="scope">
            <el-button
                v-hasPerm="['system:lpsOrderXcp:edit']"
                type="primary"
                size="small"
                link
                @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon><Edit /></template>
              编辑
            </el-button>
            <el-button
                v-hasPerm="['system:lpsOrderXcp:delete']"
                type="danger"
                size="small"
                link
                @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <!-- 新产品表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
                <el-form-item label="主键" prop="id">
                      <el-input
                          v-model="formData.id"
                          placeholder="主键"
                      />
                </el-form-item>

                <el-form-item label="莱普森订单id" prop="orderId">
                      <el-input
                          v-model="formData.orderId"
                          placeholder="莱普森订单id"
                      />
                </el-form-item>

                <el-form-item label="采购订单" prop="cgdd">
                      <el-input
                          v-model="formData.cgdd"
                          placeholder="采购订单"
                      />
                </el-form-item>

                <el-form-item label="PO行项目" prop="poxxm">
                      <el-input
                          v-model="formData.poxxm"
                          placeholder="PO行项目"
                      />
                </el-form-item>

                <el-form-item label="销售订单" prop="xsdd">
                      <el-input
                          v-model="formData.xsdd"
                          placeholder="销售订单"
                      />
                </el-form-item>

                <el-form-item label="SO行项目" prop="soxxm">
                      <el-input
                          v-model="formData.soxxm"
                          placeholder="SO行项目"
                      />
                </el-form-item>

                <el-form-item label="物料" prop="wl">
                      <el-input
                          v-model="formData.wl"
                          placeholder="物料"
                      />
                </el-form-item>

                <el-form-item label="描述" prop="ms">
                      <el-input
                          v-model="formData.ms"
                          placeholder="描述"
                      />
                </el-form-item>

                <el-form-item label="图号/版本" prop="thbb">
                      <el-input
                          v-model="formData.thbb"
                          placeholder="图号/版本"
                      />
                </el-form-item>

                <el-form-item label="数量" prop="sl">
                      <el-input
                          v-model="formData.sl"
                          placeholder="数量"
                      />
                </el-form-item>

                <el-form-item label="交货日期" prop="jhrq">
                      <el-input
                          v-model="formData.jhrq"
                          placeholder="交货日期"
                      />
                </el-form-item>

                <el-form-item label="激光打标字" prop="jgdbz">
                      <el-input
                          v-model="formData.jgdbz"
                          placeholder="激光打标字"
                      />
                </el-form-item>

                <el-form-item label="BK" prop="bk">
                      <el-input
                          v-model="formData.bk"
                          placeholder="BK"
                      />
                </el-form-item>

                <el-form-item label="TK" prop="tk">
                      <el-input
                          v-model="formData.tk"
                          placeholder="TK"
                      />
                </el-form-item>

                <el-form-item label="GQ" prop="gq">
                      <el-input
                          v-model="formData.gq"
                          placeholder="GQ"
                      />
                </el-form-item>

                <el-form-item label="P00_TSD" prop="p00Tsd">
                      <el-input
                          v-model="formData.p00Tsd"
                          placeholder="P00_TSD"
                      />
                </el-form-item>

                <el-form-item label="CODE" prop="code">
                      <el-input
                          v-model="formData.code"
                          placeholder="CODE"
                      />
                </el-form-item>

                <el-form-item label="P00_RETAINER_TYPE" prop="p00RetainerType">
                      <el-input
                          v-model="formData.p00RetainerType"
                          placeholder="P00_RETAINER_TYPE"
                      />
                </el-form-item>

                <el-form-item label="P00_INTERCOM" prop="p00Intercom">
                      <el-input
                          v-model="formData.p00Intercom"
                          placeholder="P00_INTERCOM"
                      />
                </el-form-item>

                <el-form-item label="P00_APRON_TYPE" prop="p00ApronType">
                      <el-input
                          v-model="formData.p00ApronType"
                          placeholder="P00_APRON_TYPE"
                      />
                </el-form-item>

                <el-form-item label="P00_TB_TYPE" prop="p00TbType">
                      <el-input
                          v-model="formData.p00TbType"
                          placeholder="P00_TB_TYPE"
                      />
                </el-form-item>

                <el-form-item label="P00_STRUCTURE_TYPE" prop="p00StructureType">
                      <el-input
                          v-model="formData.p00StructureType"
                          placeholder="P00_STRUCTURE_TYPE"
                      />
                </el-form-item>

                <el-form-item label="DRIVE_POS" prop="drivePos">
                      <el-input
                          v-model="formData.drivePos"
                          placeholder="DRIVE_POS"
                      />
                </el-form-item>

                <el-form-item label="备注" prop="bz">
                      <el-input type="textarea"
                                v-model="formData.bz"
                                placeholder="备注"
                      />
                </el-form-item>

                <el-form-item label="创建时间" prop="createTime">
                      <el-date-picker
                          v-model="formData.createTime"
                          type="datetime"
                          placeholder="创建时间"
                          value-format="YYYY-MM-DD HH:mm:ss"
                      />
                </el-form-item>

                <el-form-item label="创建人ID" prop="createBy">
                      <el-input
                          v-model="formData.createBy"
                          placeholder="创建人ID"
                      />
                </el-form-item>

                <el-form-item label="更新时间" prop="updateTime">
                      <el-date-picker
                          v-model="formData.updateTime"
                          type="datetime"
                          placeholder="更新时间"
                          value-format="YYYY-MM-DD HH:mm:ss"
                      />
                </el-form-item>

                <el-form-item label="修改人ID" prop="updateBy">
                      <el-input
                          v-model="formData.updateBy"
                          placeholder="修改人ID"
                      />
                </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  defineOptions({
    name: "LpsOrderXcp",
    inheritAttrs: false,
  });

  import LpsOrderXcpAPI, { LpsOrderXcpPageVO, LpsOrderXcpForm, LpsOrderXcpPageQuery } from "@/api/system/lps-order-xcp";

  const queryFormRef = ref(ElForm);
  const dataFormRef = ref(ElForm);

  const loading = ref(false);
  const removeIds = ref<number[]>([]);
  const total = ref(0);

  const queryParams = reactive<LpsOrderXcpPageQuery>({
    pageNum: 1,
    pageSize: 10,
  });

  // 新产品表格数据
  const pageData = ref<LpsOrderXcpPageVO[]>([]);

  // 弹窗
  const dialog = reactive({
    title: "",
    visible: false,
  });

  // 新产品表单数据
  const formData = reactive<LpsOrderXcpForm>({});

  // 新产品表单校验规则
  const rules = reactive({
  });

  /** 查询新产品 */
  function handleQuery() {
    loading.value = true;
          LpsOrderXcpAPI.getPage(queryParams)
        .then((data) => {
          pageData.value = data.list;
          total.value = data.total;
        })
        .finally(() => {
          loading.value = false;
        });
  }

  /** 重置新产品查询 */
  function handleResetQuery() {
    queryFormRef.value!.resetFields();
    queryParams.pageNum = 1;
    handleQuery();
  }

  /** 行复选框选中记录选中ID集合 */
  function handleSelectionChange(selection: any) {
    removeIds.value = selection.map((item: any) => item.id);
  }

  /** 打开新产品弹窗 */
  function handleOpenDialog(id?: number) {
    dialog.visible = true;
    if (id) {
      dialog.title = "修改新产品";
            LpsOrderXcpAPI.getFormData(id).then((data) => {
        Object.assign(formData, data);
      });
    } else {
      dialog.title = "新增新产品";
    }
  }

  /** 提交新产品表单 */
  function handleSubmit() {
    dataFormRef.value.validate((valid: any) => {
      if (valid) {
        loading.value = true;
        const id = formData.id;
        if (id) {
                LpsOrderXcpAPI.update(id, formData)
              .then(() => {
                ElMessage.success("修改成功");
                handleCloseDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        } else {
                LpsOrderXcpAPI.add(formData)
              .then(() => {
                ElMessage.success("新增成功");
                handleCloseDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        }
      }
    });
  }

  /** 关闭新产品弹窗 */
  function handleCloseDialog() {
    dialog.visible = false;
    dataFormRef.value.resetFields();
    dataFormRef.value.clearValidate();
    formData.id = undefined;
  }

  /** 删除新产品 */
  function handleDelete(id?: number) {
    const ids = [id || removeIds.value].join(",");
    if (!ids) {
      ElMessage.warning("请勾选删除项");
      return;
    }

    ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(
        () => {
          loading.value = true;
                LpsOrderXcpAPI.deleteByIds(ids)
              .then(() => {
                ElMessage.success("删除成功");
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        },
        () => {
          ElMessage.info("已取消删除");
        }
    );
  }

  onMounted(() => {
    handleQuery();
  });
</script>
